using System;
using System.Collections.Generic;
using System.Text;
using Ghasam.Business.Domain.Entity;
using NHibernate;
using NHibernate.Expression;

namespace Ghasam.Business.DAO.Impl
{
    public class AccountingDao : DaoBase<Accounting>, IAccountingDao
    {
        public IList<Accounting> FindAccounts(int? customerId, bool isFormal, DateTime? startDate, DateTime? endDate)
        {
            ICriteria cri = base.Session.CreateCriteria(typeof(Accounting));
            cri.CreateAlias("Customer", "customer");

            if (customerId.HasValue && customerId > 0)
            {
                cri.Add(Expression.Eq("customer.Id", customerId));
            }

            if (startDate.HasValue)
            {
                cri.Add(Expression.Ge("AccountingDate", startDate.Value));
            }

            if (endDate.HasValue)
            {
                cri.Add(Expression.Le("AccountingDate", endDate.Value));
            }

            cri.Add(Expression.Eq("IsFormal", isFormal));
            return cri.List<Accounting>();
        }
        public IList<Accounting> FindAccountsBySorting(int? customerId, bool isFormal, DateTime? startDate, DateTime? endDate, string param, bool asc)
        {
            ICriteria cri = base.Session.CreateCriteria(typeof(Accounting));
            cri.CreateAlias("Customer", "customer");

            if (customerId.HasValue && customerId > 0)
            {
                cri.Add(Expression.Eq("customer.Id", customerId));
            }

            if (startDate.HasValue)
            {
                cri.Add(Expression.Ge("AccountingDate", startDate.Value));
            }

            if (endDate.HasValue)
            {
                cri.Add(Expression.Le("AccountingDate", endDate.Value));
            }
            Order dir = new Order(param, asc);

            cri.Add(Expression.Eq("IsFormal", isFormal));
            cri.AddOrder(dir);
            return cri.List<Accounting>();
        }

    }
}
